package com.glory.hzlionpda.activity;

import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.hzlionpda.R;
import com.glory.hzlionpda.base.BasePollingActivity;
import com.glory.hzlionpda.base.Constant;
import com.glory.hzlionpda.bean.EqpBean;
import com.glory.hzlionpda.utils.NetUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;

public class SearchEqpActivity extends BasePollingActivity {


    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.tvStatus)
    TextView tvStatus;
    @BindView(R.id.tvLotID)
    TextView tvLotID;
    @BindView(R.id.tvRecipe)
    TextView tvRecipe;
    @BindView(R.id.tvLotNum)
    TextView tvLotNum;
    @BindView(R.id.tvCurrentStation)
    TextView tvCurrentStation;
    @BindView(R.id.tvNextStation)
    TextView tvNextStation;
    @BindView(R.id.llContent)
    LinearLayout llContent;
    @BindView(R.id.llMain)
    LinearLayout llMain;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_search_eqp;
    }

    @Override
    protected void initView() {
        freeUI_titleView.setTitle("设备作业量查询");
    }

    @Override
    protected void initData() {
        etLot.requestFocus();

        etLot.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                return event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER);
            }
        });
        etLot.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (start == 0 && before == 0 && count > 1) {
                    if (s.length() > 0) {
                        requestEqpInfo(s.toString());
                    } else {
                        showToast("扫描信息有误", false);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void requestEqpInfo(String string) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("EQPID", string);
            jsonObject.put("OPID", Constant.CURRENT_USER);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestGetNet("EQPSTATUSREQUEST", this, jsonObject, new FreeUI_DialogEntityCallBack<EqpBean>(
                new TypeToken<EqpBean>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<EqpBean> response) {
                if (response.body().isSuccess(getApplicationContext())) {
                    if (response.body() != null) {
                        llContent.setVisibility(View.VISIBLE);
                        setEqpInfo(response.body());
                    }
                }else {
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            SearchEqpActivity.this.finish();
                        }
                    }, 2000);
                }
            }

            @Override
            public void onError(Response<EqpBean> response) {
                super.onError(response);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        SearchEqpActivity.this.finish();
                    }
                }, 2000);
                loadError(response.getException(), "EQPSTATUSREQUEST");
            }
        });
    }

    private void setEqpInfo(EqpBean eqpBean) {
        tvStatus.setText(eqpBean.getEQPSTATUS());
        tvLotID.setText(eqpBean.getLOTID());
        tvRecipe.setText(eqpBean.getRECIPE());
        tvLotNum.setText(eqpBean.getNOWLOTNUM());
        tvCurrentStation.setText(eqpBean.getNOWSTEP());
        tvNextStation.setText(eqpBean.getNEXTSTEP());
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return new FreeUI_AddViewEntity(getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY), llMain);
    }

}

